package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.Models.ClassSettingsModel;
import pl.edu.pw.MidEmu.UI.Controllers.ClassLoaderController;
import pl.edu.pw.MidEmu.UI.Views.ApplicationsListView;

/**
 * Frame with GUI for creating new MIDAS applications
 * @author Mateusz Starodab
 *
 */
public class ClassLoaderFrame extends JDialog
{
	private static final long serialVersionUID = 1247046052530400922L;

	private JButton _btnCancel;

	private JButton _btnOK;

	private ClassLoaderController _classLoaderController;
	private JLabel _lblClassName;
	private JLabel _lblClassPath;

	private JLabel _lblCommandLineParameters;
	private JLabel _lblPackage;

	private JLabel _lblParameters;
	private JLabel _lblWorkDir;
	private JPanel _pnlCenter;
	private JPanel _pnlMain;
	private JPanel _pnlSouth;
	private JTextField _tbxClassName;

	private JTextArea _tbxClassPath;
	private JTextArea _tbxCommandLineParameters;
	private JTextField _tbxPackage;
	private JTextField _tbxParameters;
	private JTextField _tbxWorkDir;
	private ClassSettingsModel model;

	private MidasComponent parent;
	private ApplicationsListView secondParent;

	public ClassLoaderFrame(ClassSettingsModel model, MidasComponent parent, ApplicationsListView secondParent)
	{
		this.model = model;
		this.parent = parent;
		this.secondParent = secondParent;

		setTitle(Consts.MainFrame_Title + " - " + this.parent.getName() + " - " + Consts.ClassLoader_Title);
		setSize(480, 510);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setModal(true);
		setLocationByPlatform(true);

		_classLoaderController = new ClassLoaderController(this);

		_pnlMain = new JPanel();
		_pnlMain.setLayout(new BorderLayout());

		_pnlSouth = new JPanel();
		_pnlSouth.setLayout(new FlowLayout());

		_btnOK = new JButton(Consts.ClassLoader_OKButton);
		_btnOK.addActionListener(_classLoaderController);

		_btnCancel = new JButton(Consts.ClassLoader_CancelButton);
		_btnCancel.addActionListener(_classLoaderController);

		_pnlCenter = new JPanel();
		_pnlCenter.setLayout(new FlowLayout(FlowLayout.LEFT));

		_pnlCenter.setBorder(BorderFactory.createTitledBorder(Consts.ClassLoader_LoadClassLabel));

		_tbxWorkDir = new JTextField(40);
		_tbxWorkDir.setName("_tbxWorkDir");
		_tbxWorkDir.getDocument().addDocumentListener(_classLoaderController);
		_lblWorkDir = new JLabel(Consts.ClassLoader_WorkDirLabel);
		_lblWorkDir.setLabelFor(_tbxWorkDir);

		_tbxPackage = new JTextField(40);
		_lblPackage = new JLabel(Consts.ClassLoader_PackageLabel);
		_lblPackage.setLabelFor(_tbxPackage);

		_tbxClassName = new JTextField(40);
		_lblClassName = new JLabel(Consts.ClassLoader_ClassNameLabel);
		_lblClassName.setLabelFor(_tbxClassName);

		_tbxParameters = new JTextField(40);
		_lblParameters = new JLabel(Consts.ClassLoader_ParametersLabel);
		_lblParameters.setLabelFor(_tbxParameters);

		_tbxCommandLineParameters = new JTextArea(6, 40);
		_tbxCommandLineParameters.setBorder(_tbxClassName.getBorder());
		_tbxCommandLineParameters.setLineWrap(true);
		_lblCommandLineParameters = new JLabel(Consts.ClassLoader_CommandLineLabel);
		_lblCommandLineParameters.setLabelFor(_tbxCommandLineParameters);

		_tbxClassPath = new JTextArea(6, 40);
		_tbxClassPath.setBorder(_tbxClassName.getBorder());
		_tbxClassPath.setLineWrap(true);
		_lblClassPath = new JLabel(Consts.ClassLoader_ClassPathLabel);
		_lblClassPath.setLabelFor(_tbxClassPath);

		_pnlCenter.add(_lblWorkDir);
		_pnlCenter.add(_tbxWorkDir);

		_pnlCenter.add(_lblPackage);
		_pnlCenter.add(_tbxPackage);

		_pnlCenter.add(_lblClassName);
		_pnlCenter.add(_tbxClassName);

		_pnlCenter.add(_lblParameters);
		_pnlCenter.add(_tbxParameters);

		_pnlCenter.add(_lblCommandLineParameters);
		_pnlCenter.add(_tbxCommandLineParameters);

		_pnlCenter.add(_lblClassPath);
		_pnlCenter.add(_tbxClassPath);

		_pnlMain.add(_pnlCenter, BorderLayout.CENTER);

		_pnlSouth.add(_btnOK);
		_pnlSouth.add(_btnCancel);
		_pnlMain.add(_pnlSouth, BorderLayout.SOUTH);

		setContentPane(_pnlMain);

		_tbxClassName.setText(this.model.get_className());
		_tbxCommandLineParameters.setText(this.model.get_JVMparameters());
		_tbxParameters.setText(this.model.get_parameters());
		_tbxPackage.setText(this.model.get_package());
		_tbxWorkDir.setText(parent.getController().getMidas().getTemporaryDir());
		_tbxClassPath.setText(this.model.get_classPath());

		if (this.model.get_package() != null && this.model.get_package() != "")
			_classLoaderController.setPackageNameChanged();
		else
			_classLoaderController.setOrginalWorkDir(this.model.get_workDir());

		_classLoaderController.setOrginalFilePath(this.model.get_path());
	}

	public ApplicationsListView getApplicationListParent()
	{
		return secondParent;
	}

	public String getClassName()
	{
		return _tbxClassName.getText();
	}

	public String getClassPath()
	{
		return _tbxClassPath.getText();
	}

	public String getCommandLineParameters()
	{
		return _tbxCommandLineParameters.getText();
	}

	public MidasComponent getMidasComponentParent()
	{
		return parent;
	}

	public String getPackageName()
	{
		return _tbxPackage.getText();
	}

	public String getParameters()
	{
		return _tbxParameters.getText();
	}

	public String getWorkDir()
	{
		return _tbxWorkDir.getText();
	}

	public boolean isMidasRunning()
	{
		return parent.getController().isMidasRunning();
	}

	public void reportMidasError()
	{
		JOptionPane.showMessageDialog(this, "Can't start application without running MIDAS environment", "Invalid operation", JOptionPane.ERROR_MESSAGE);
	}

	public void reportValidationErrors()
	{
		JOptionPane.showMessageDialog(this, "Application configuration is invalid.\nMake sure you fill all necessary fields", "Invalid configuration", JOptionPane.ERROR_MESSAGE);
	}

	public void setPackageName(String packageName)
	{
		StringBuffer buffer = new StringBuffer(_tbxPackage.getText());
		buffer.insert(0, packageName);

		_tbxPackage.setText(buffer.toString());
	}

	public boolean validateApplication()
	{
		if (getClassName().equals(""))
			return false;
		if (getPackageName().equals(""))
			return false;
		if (getWorkDir().equals(""))
			return false;
		return true;
	}
}
